
//
//  LocationGetObj.m
//  JxCarios
//
//  Created by xcq on 15/1/31.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "LocationGetObj.h"


@implementation LocationGetObj

- (id)init{
    self = [super init];
    if (self) {
        
    }
    return  self;
}

+ (LocationGetObj *)locationObj {
    LocationGetObj *obj = [LocationGetObj new];
    return obj;
}

- (void)startLocation {
    //设置定位精确度，默认：kCLLocationAccuracyBest
    [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    //指定最小距离更新(米)，默认：kCLDistanceFilterNone
    [BMKLocationService setLocationDistanceFilter:100.f];
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
}

- (void)stopLocation{
    if (!_locService) {
        return;
    }
    [_locService stopUserLocationService];
}


- (BOOL)startGeoCode:(BMKGeoCodeSearchOption *)option{
    if (!_geoSearch) {
        _geoSearch = [[BMKGeoCodeSearch alloc] init];
        _geoSearch.delegate = self;
    }
   return [_geoSearch geoCode:option];
    
}

- (BOOL)startReverseGeoCode:(BMKReverseGeoCodeOption *)option{
    if (!_geoSearch) {
        _geoSearch = [[BMKGeoCodeSearch alloc] init];
        _geoSearch.delegate = self;
    }
    
    return  [_geoSearch reverseGeoCode:option];
}

//根据地址返回经纬度
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if ([_geoDelegate respondsToSelector:@selector(onGetGeoCodeResultSuccess:result:errorCode:)]) {
        [_geoDelegate onGetGeoCodeResultSuccess:searcher result:result errorCode:error];
    }
    
}
//根据经纬度返回地址
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if ([_geoDelegate respondsToSelector:@selector(onGetReverseGeoCodeResultSuccess:result:errorCode:)]) {
        [_geoDelegate onGetReverseGeoCodeResultSuccess:searcher result:result errorCode:error];
    }
}

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    if([_delegate respondsToSelector:@selector(getLocationSuccess:)]){
        [_delegate getLocationSuccess:userLocation];
    }
    
}

- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation{
    if([_delegate respondsToSelector:@selector(getLocationWithHeadingIsChange:)]){
        [_delegate getLocationWithHeadingIsChange:userLocation];
    }
}

- (void)didFailToLocateUserWithError:(NSError *)error{
    NSString *errorString;
    [self stopLocation];
    switch([error code]) {
        case kCLErrorDenied:
            //Access denied by user
            [[[UIAlertView alloc] initWithTitle:@"提示" message:@"亲，请在设置->隐私->定位服务中打开本应用的位置服务！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            //Do something...
            break;
        case kCLErrorLocationUnknown:
            //Probably temporary...
            errorString = @"Location data unavailable";
            //Do something else...
            break;
        default:
            errorString = @"An unknown error has occurred";
            break;
    }
    if([_delegate respondsToSelector:@selector(getLocationFail:)]){
        [_delegate getLocationFail:error];
    }
}

- (void)willStartLocatingUser{
    if([_delegate respondsToSelector:@selector(willStartLocation)]){
        [_delegate willStartLocation];
    }
}

- (void)didStopLocatingUser{
    if([_delegate respondsToSelector:@selector(didStopLocation)]){
        [_delegate didStopLocation];
    }}
@end
